package generation.scale;

import util.Arquivo;
import util.Utils;

public class ScaleFactory {

	public static Scale getScale(String scaleName){
		//return readFromFile(ConfigFile.getString("fileScales"), scaleName);
		return readFromFile("scales.input", scaleName);
	}

	/**
	 * Method that read a String format which represents a scale
	 * "101011010101" represents a Major scale;
	 * "| C | C# | D | D# | E | F | F# | G | G# | A | A# | B |"
	 * "| C | -- | D | -- | E | F | -- | G | -- | A | -- | B |"
	 * "| 1 | 0  | 1 | 0  | 1 | 1 | 0  | 1 | 0  | 1 | 0  | 1 |"
	 * @return
	 */
	private static String readScaleInput(String input){
		String intervalNotation = "";
		for (int i = 0; i < input.length(); i++) {
			if(input.charAt(i) == '1'){
				intervalNotation = intervalNotation + "<" + (i+1) + "> ";
			}
		}		
		return intervalNotation;
	}

	private static Scale readFromFile(String filename, String scaleName){
		Arquivo file = new Arquivo(filename, "out.out");
		String[] tunes = null;
		String[] chords = null;
		while(!file.isEndOfFile()){
			String scaleNameFile = file.readString();
			if(scaleNameFile.equals(scaleName)){
				String scaleBinaryRepresentation = file.readString();
				String scaleChords = file.readString();
				tunes = Utils.getArrayFromString(readScaleInput(scaleBinaryRepresentation), " ");
				chords = Utils.getArrayFromString(scaleChords, ",");				
			} else {
				file.readString();
				file.readString();
			}
		}
		return new Scale(scaleName, tunes, chords);
	}

	public static void main(String[] args) {
		Scale scale = ScaleFactory.getScale("pentmaj");
		System.out.println(scale);
		//sp.readFromFile("src/scales.input", "min");
		/*String intervalNotation = sp.readScaleInput("101011010101");
		StringTokenizer tokenizer = new StringTokenizer(intervalNotation, " ");
		System.out.println("Tokens quantity: " + tokenizer.countTokens());
		System.out.println(intervalNotation);*/

	}

}
